import Vue from 'vue';
//导入数据操作API
import api from "@/service/api";
import http from "@/service/http";

//状态
const state={
	classifyId:'',
	//
	classifys:[], 
	//表格选择行数据
	selects:[],
	//总记录数
	total:0, 
	//当前页
	currentPage:1, 
	//每页显示记录数
	pageSize:10, 
	//表格加载状态
	listLoading:false,  
	isToSelect:false,//是否来自选择
	ableTag:'1',//1:可用
	classifyItem:{//选中的
		id:'',
		name:'',
		pid:'',
		child:[]
	},
	isDialogShow:false
};

//getter
const getters={
	groupId:state=>state.groupId,
	classifys:state=>state.classifys,
	selects:state=>state.selects,
	listLoading:state=>state.listLoading,
	selectItem:state=>state.selectItem,
	classifyId:state=>state.classifyId
};

//action
const actions={
	//列表
	classifyList({commit,state}){
		let param={
			groupId:state.groupId,
			pageNum:state.currentPage,
			pageSize:state.pageSize,
			status:state.ableTag
		};
		commit('listLoading',true);
		http.post(api.sysApi.classifyList,'').then(data => {
			if(!data.success){
				return;
			}
            commit('loadList',data);
			commit('listLoading',false);
        }); 
	},
	//选择group
	selectItem({commit,dispatch},classifyId){
		commit('selectGroup',classifyId);
	},
	//表格载入状态
	listLoading({commit},isShow){
		commit('listLoading',isShow);
	},
	//是否来自选择用户
	toSelect({commit},isToSelect){
		commit('isSelect',isToSelect);
	},
	//是否显示为dialog
	showDialog({commit},isDialogShow){
		commit('setDialogShow',isDialogShow);
	}
};

//mutations
const mutations={
	loadList(state,param){
		state.classifys=param.data;
	},
	selectGroup(state,classifyId){
		state.classifyId=classifyId;
	},
	//是否来自选择用户
	isSelect(state,isToSelect){
		state.isToSelect=isToSelect;
		state.ableTag = isToSelect?1:'';
	},
	listLoading(state,isShow){
		state.listLoading=isShow;
	},
	setDialogShow(state,isDialogShow){
		state.isDialogShow=isDialogShow;
	}
};

//导出
export default{
	//启用命名空间
	namespaced:true,
	state,
	getters,
	actions,
	mutations
};